package com.css.zfzx.fxpg.modules.buildings.census.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.css.bpm.platform.api.local.PlatformAPI;
import com.css.bpm.platform.org.division.repository.entity.DivisionEntity;
import com.css.bpm.platform.sys.dict.repository.entity.DictItemEntity;
import com.css.bpm.platform.utils.PlatformDateUtils;
import com.css.bpm.platform.utils.PlatformObjectUtils;
import com.css.bpm.platform.utils.PlatformPageUtils;
import com.css.bpm.platform.utils.PlatformSessionUtils;
import com.css.zfzx.fxpg.common.utils.CheckObjFields;
import com.css.zfzx.fxpg.modules.buildings.census.repository.*;
import com.css.zfzx.fxpg.modules.buildings.census.repository.entity.*;
import com.css.zfzx.fxpg.modules.buildings.census.service.FxCensusBuildingService;
import com.css.zfzx.fxpg.modules.buildings.census.viewobjects.FxHouseQueryParams;
import com.css.zfzx.fxpg.modules.buildings.data.repository.entity.FxBuildingsEntity;
import com.css.zfzx.fxpg.modules.middle.repository.FxMiddleResultNativeRepository;
import com.css.zfzx.fxpg.modules.middle.repository.FxMiddleResultRepository;
import com.css.zfzx.fxpg.modules.middle.viewobjects.FxMiddleResultQueryParams;
import com.css.zfzx.fxpg.modules.vulnerability.province.repository.FxVulnerabilityProvinceNativeRepository;
import com.css.zfzx.fxpg.modules.vulnerability.province.repository.entity.FxVulnerabilityProvinceEntity;
import com.css.zfzx.fxpg.modules.vulnerability.province.viewobjects.FxVulnerabilityProvinceQueryParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author andy
 * @date 2020-11-11
 */
@Service
public class FxCensusBuildingServiceImpl implements FxCensusBuildingService {

    @Autowired
    private FxVulnerabilityProvinceNativeRepository fxVulnerabilityProvinceNativeRepository;
    @Autowired
    private FxCityHouseNativeRepository fxCityHouseNativeRepository;
    @Autowired
    private FxCityHouseRepository fxCityHouseRepository;
    @Autowired
    private FxCityNonHouseNativeRepository fxCityNonHouseNativeRepository;
    @Autowired
    private FxCityNonHouseRepository fxCityNonHouseRepository;
    @Autowired
    private FxCountryDlHouseNativeRepository fxCountryDlHouseNativeRepository;
    @Autowired
    private FxCountryDlHouseRepository fxCountryDlHouseRepository;
    @Autowired
    private FxCountryFzHouseNativeRepository fxCountryFzHouseNativeRepository;
    @Autowired
    private FxCountryJhHouseNativeRepository fxCountryJhHouseNativeRepository;
    @Autowired
    private FxCountryJhHouseRepository fxCountryJhHouseRepository;
    @Autowired
    private FxCountryNonHouseNativeRepository fxCountryNonHouseNativeRepository;
    @Autowired
    private FxCountryNonHouseRepository fxCountryNonHouseRepository;
    @Autowired
    private FxMiddleResultNativeRepository fxMiddleResultNativeRepository;
    @Autowired
    private FxMiddleResultRepository fxMiddleResultRepository;

    private Map<String, Map<String, String>> tableTojglx2Name = new HashMap<>();
    private Map<String, Map<String, String>> tableTofwyt2Name = new HashMap<>();
    private Map<String, Map<String, String>> tableTomqsfld2Name = new HashMap<>();
    private Map<String, Map<String, String>> divisionCodeToName = new HashMap<>();

    @Override
    public JSONObject queryFxCensusHouses(FxHouseQueryParams fxHouseQueryParams, int curPage, int pageSize, String sort, String order, String userId) {
        if (!PlatformObjectUtils.isEmpty(sort) && sort.length() > 0) {
            sort = CheckObjFields.propertyChange(sort);
        }
        JSONObject jsonObject = new JSONObject();
        String houseType = fxHouseQueryParams.getHouseType();
        if ("city_house".equals(houseType)) {
            jsonObject = queryFxCensusCityHouses(fxHouseQueryParams, curPage, pageSize, sort, order, userId);
        } else if ("city_non_house".equals(houseType)) {
            jsonObject = queryFxCensusCityNonHouses(fxHouseQueryParams, curPage, pageSize, sort, order, userId);
        } else if ("country_dl_house".equals(houseType)) {
            jsonObject = queryFxCensusCountryDlHouses(fxHouseQueryParams, curPage, pageSize, sort, order, userId);
        } else if ("country_jh_house".equals(houseType)) {
            jsonObject = queryFxCensusCountryJhHouses(fxHouseQueryParams, curPage, pageSize, sort, order, userId);
        } else if ("country_non_house".equals(houseType)) {
            jsonObject = queryFxCensusCountryNonHouses(fxHouseQueryParams, curPage, pageSize, sort, order, userId);
        }
        return jsonObject;
    }
    private JSONObject queryFxCensusCityHouses(FxHouseQueryParams fxHouseQueryParams, int curPage, int pageSize, String sort, String order, String userId) {

        String province = "", city = "", area = "";
        List<FxBuildingsEntity> fxBuildings = new ArrayList<>();

        Page<FxCityHouseEntity> fxCityHouses = fxCityHouseNativeRepository.queryFxCityHouses(fxHouseQueryParams, curPage, pageSize, sort, order, userId);
        List<FxCityHouseEntity> fxCityHouseList = fxCityHouses.getContent();
        FxCityHouseEntity fxCityHouseEntity = null;
        for (int i = 0; i < fxCityHouseList.size(); i++) {
            fxCityHouseEntity = fxCityHouseList.get(i);

            String id = fxCityHouseEntity.getBh();

            province = fxCityHouseEntity.getProvince();
            city = fxCityHouseEntity.getCity();
            area = fxCityHouseEntity.getDistrict();

            province = getDivisionNameByCode("province", province);
            city = getDivisionNameByCode("city", city);
            area = getDivisionNameByCode("district", area);

            String buildingName = fxCityHouseEntity.getJzmc();
            String mj = fxCityHouseEntity.getDcmj();
            Float dcmj = (mj == null ? 0.0f : new Float(mj));//建筑面积
            String czfwjglx = fxCityHouseEntity.getCzfwjglx();//结构类型
            czfwjglx = getJglxName("city_house", czfwjglx);
            Integer peopleNumber = 0;
            String purpose = "";
            Integer cs = fxCityHouseEntity.getDscs();
            String buildingTime = fxCityHouseEntity.getBuildTime();
            String currentIntensity = fxCityHouseEntity.getMqsfld();
            currentIntensity = getMqsfldName("city_house", currentIntensity);

            //province, city, area, buildingArea, structureType, buildingName, purpose, buildingTime, currentIntensity
            FxBuildingsEntity fxBuildingsEntity = wrapBuildingEntity(id, province, city, area, dcmj, czfwjglx, buildingName, cs, peopleNumber, purpose, buildingTime, currentIntensity);

            fxBuildings.add(fxBuildingsEntity);
        }

        Pageable pageable = PageRequest.of(curPage - 1, pageSize);
        Page<FxBuildingsEntity> fxBuildingsPage = new PageImpl<>(fxBuildings, pageable, fxCityHouses.getTotalElements());
        JSONObject jsonObject = PlatformPageUtils.formatPageData(fxBuildingsPage);
        return jsonObject;
    }
    private JSONObject queryFxCensusCityNonHouses(FxHouseQueryParams fxHouseQueryParams, int curPage, int pageSize, String sort, String order, String userId) {

        String province = "", city = "", area = "";
        List<FxBuildingsEntity> fxBuildings = new ArrayList<>();

        Page<FxCityNonHouseEntity> fxCityNonHouses = fxCityNonHouseNativeRepository.queryFxCityNonHouses(fxHouseQueryParams, curPage, pageSize, sort, order, userId);
        List<FxCityNonHouseEntity> fxCityNonHouseList = fxCityNonHouses.getContent();
        FxCityNonHouseEntity fxCityNonHouseEntity = null;
        for (int i = 0; i < fxCityNonHouseList.size(); i++) {
            fxCityNonHouseEntity = fxCityNonHouseList.get(i);

            String id = fxCityNonHouseEntity.getBh();

            province = fxCityNonHouseEntity.getProvince();
            city = fxCityNonHouseEntity.getCity();
            area = fxCityNonHouseEntity.getDistrict();

            province = getDivisionNameByCode("province", province);
            city = getDivisionNameByCode("city", city);
            area = getDivisionNameByCode("district", area);

            String buildingName = fxCityNonHouseEntity.getJzmc();
            String mj = fxCityNonHouseEntity.getDcmj();
            Float dcmj = (mj == null ? 0.0f : new Float(mj));//建筑面积
            String czfwjglx = fxCityNonHouseEntity.getCzfwjglx();//结构类型
            czfwjglx = getJglxName("city_non_house", czfwjglx);
            Integer cs = fxCityNonHouseEntity.getDscs();
            Integer peopleNumber = 0;
            String purpose = String.valueOf(fxCityNonHouseEntity.getFwyt());
            purpose = getFwytName("city_non_house", purpose);
            String buildingTime = fxCityNonHouseEntity.getBuildTime();
            String currentIntensity = fxCityNonHouseEntity.getMqsfld();
            currentIntensity = getMqsfldName("city_non_house", currentIntensity);

            //province, city, area, buildingArea, structureType, buildingName, purpose, buildingTime, currentIntensity
            FxBuildingsEntity fxBuildingsEntity = wrapBuildingEntity(id, province, city, area, dcmj, czfwjglx, buildingName, cs, peopleNumber, purpose, buildingTime, currentIntensity);

            fxBuildings.add(fxBuildingsEntity);
        }

        Pageable pageable = PageRequest.of(curPage - 1, pageSize);
        Page<FxBuildingsEntity> fxBuildingsPage = new PageImpl<>(fxBuildings, pageable, fxCityNonHouses.getTotalElements());
        JSONObject jsonObject = PlatformPageUtils.formatPageData(fxBuildingsPage);
        return jsonObject;
    }
    private JSONObject queryFxCensusCountryDlHouses(FxHouseQueryParams fxHouseQueryParams, int curPage, int pageSize, String sort, String order, String userId) {

        String province = "", city = "", area = "";
        List<FxBuildingsEntity> fxBuildings = new ArrayList<>();

        Page<FxCountryDlHouseEntity> fxCountryDlHouses = fxCountryDlHouseNativeRepository.queryFxCountryDlHouses(fxHouseQueryParams, curPage, pageSize, sort, order, userId);
        List<FxCountryDlHouseEntity> fxCountryDlHouseList = fxCountryDlHouses.getContent();
        FxCountryDlHouseEntity fxCountryDlHouseEntity = null;
        for (int i = 0; i < fxCountryDlHouseList.size(); i++) {
            fxCountryDlHouseEntity = fxCountryDlHouseList.get(i);

            String id = fxCountryDlHouseEntity.getBh();

            province = fxCountryDlHouseEntity.getProvince();
            city = fxCountryDlHouseEntity.getCity();
            area = fxCountryDlHouseEntity.getDistrict();

            province = getDivisionNameByCode("province", province);
            city = getDivisionNameByCode("city", city);
            area = getDivisionNameByCode("district", area);

            String buildingName = fxCountryDlHouseEntity.getAddress();
            String mj = fxCountryDlHouseEntity.getDcmj();
            Float dcmj = (mj == null ? 0.0f : new Float(mj));//建筑面积
            String czfwjglx = fxCountryDlHouseEntity.getJglx();//结构类型
            czfwjglx = getJglxName("country_dl_house", czfwjglx);
            Integer cs = fxCountryDlHouseEntity.getCs();
            Integer peopleNumber = fxCountryDlHouseEntity.getCzrk();
            String purpose = "";
            String buildingTime = fxCountryDlHouseEntity.getBuildTime();
            String currentIntensity = "";

            //province, city, area, buildingArea, structureType, buildingName, purpose, buildingTime, currentIntensity
            FxBuildingsEntity fxBuildingsEntity = wrapBuildingEntity(id, province, city, area, dcmj, czfwjglx, buildingName, cs, peopleNumber, purpose, buildingTime, currentIntensity);

            fxBuildings.add(fxBuildingsEntity);
        }

        Pageable pageable = PageRequest.of(curPage - 1, pageSize);
        Page<FxBuildingsEntity> fxBuildingsPage = new PageImpl<>(fxBuildings, pageable, fxCountryDlHouses.getTotalElements());
        JSONObject jsonObject = PlatformPageUtils.formatPageData(fxBuildingsPage);
        return jsonObject;
    }
    private JSONObject queryFxCensusCountryJhHouses(FxHouseQueryParams fxHouseQueryParams, int curPage, int pageSize, String sort, String order, String userId) {

        String province = "", city = "", area = "";
        List<FxBuildingsEntity> fxBuildings = new ArrayList<>();

        Page<FxCountryJhHouseEntity> fxCountryJhHouses = fxCountryJhHouseNativeRepository.queryFxCountryJhHouses(fxHouseQueryParams, curPage, pageSize, sort, order, userId);
        List<FxCountryJhHouseEntity> fxCountryJhHouseList = fxCountryJhHouses.getContent();
        FxCountryJhHouseEntity fxCountryJhHouseEntity = null;
        for (int i = 0; i < fxCountryJhHouseList.size(); i++) {
            fxCountryJhHouseEntity = fxCountryJhHouseList.get(i);

            String id = fxCountryJhHouseEntity.getBh();

            province = fxCountryJhHouseEntity.getProvince();
            city = fxCountryJhHouseEntity.getCity();
            area = fxCountryJhHouseEntity.getDistrict();
            String buildingName = fxCountryJhHouseEntity.getJzmc();
            String mj = fxCountryJhHouseEntity.getDcmj();
            Float dcmj = (mj == null ? 0.0f : new Float(mj));//建筑面积
            String czfwjglx = fxCountryJhHouseEntity.getJglx();//结构类型
            czfwjglx = getJglxName("country_jh_house", czfwjglx);
            Integer cs = fxCountryJhHouseEntity.getDscs();
            Integer peopleNumber = 0;
            String purpose = "";
            String buildingTime = fxCountryJhHouseEntity.getBuildTime();
            String currentIntensity = "";

            //province, city, area, buildingArea, structureType, buildingName, purpose, buildingTime, currentIntensity
            FxBuildingsEntity fxBuildingsEntity = wrapBuildingEntity(id, province, city, area, dcmj, czfwjglx, buildingName, cs, peopleNumber, purpose, buildingTime, currentIntensity);

            fxBuildings.add(fxBuildingsEntity);
        }

        Pageable pageable = PageRequest.of(curPage - 1, pageSize);
        Page<FxBuildingsEntity> fxBuildingsPage = new PageImpl<>(fxBuildings, pageable, fxCountryJhHouses.getTotalElements());
        JSONObject jsonObject = PlatformPageUtils.formatPageData(fxBuildingsPage);
        return jsonObject;
    }
    private JSONObject queryFxCensusCountryNonHouses(FxHouseQueryParams fxHouseQueryParams, int curPage, int pageSize, String sort, String order, String userId) {

        String province = "", city = "", area = "";
        List<FxBuildingsEntity> fxBuildings = new ArrayList<>();

        Page<FxCountryNonHouseEntity> fxCountryNonHouses = fxCountryNonHouseNativeRepository.queryFxCountryNonHouses(fxHouseQueryParams, curPage, pageSize, sort, order, userId);
        List<FxCountryNonHouseEntity> fxCountryNonHouseList = fxCountryNonHouses.getContent();
        FxCountryNonHouseEntity fxCountryNonHouseEntity = null;
        for (int i = 0; i < fxCountryNonHouseList.size(); i++) {
            fxCountryNonHouseEntity = fxCountryNonHouseList.get(i);

            String id = fxCountryNonHouseEntity.getBh();

            province = fxCountryNonHouseEntity.getProvince();
            city = fxCountryNonHouseEntity.getCity();
            area = fxCountryNonHouseEntity.getDistrict();

            province = getDivisionNameByCode("province", province);
            city = getDivisionNameByCode("city", city);
            area = getDivisionNameByCode("district", area);

            String buildingName = fxCountryNonHouseEntity.getJzmc();
            String mj = fxCountryNonHouseEntity.getDcmj();
            Float dcmj = (mj == null ? 0.0f : new Float(mj));//建筑面积
            String czfwjglx = fxCountryNonHouseEntity.getJglx();//结构类型
            czfwjglx = getJglxName("country_non_house", czfwjglx);
            Integer cs = fxCountryNonHouseEntity.getDscs();
            Integer peopleNumber = 0;
            String purpose = String.valueOf(fxCountryNonHouseEntity.getFwyt());
            purpose = getFwytName("country_non_house", purpose);
            String buildingTime = fxCountryNonHouseEntity.getBuildTime();
            String currentIntensity = "";

            //province, city, area, buildingArea, structureType, buildingName, purpose, buildingTime, currentIntensity
            FxBuildingsEntity fxBuildingsEntity = wrapBuildingEntity(id, province, city, area, dcmj, czfwjglx, buildingName, cs, peopleNumber, purpose, buildingTime, currentIntensity);

            fxBuildings.add(fxBuildingsEntity);
        }

        Pageable pageable = PageRequest.of(curPage - 1, pageSize);
        Page<FxBuildingsEntity> fxBuildingsPage = new PageImpl<>(fxBuildings, pageable, fxCountryNonHouses.getTotalElements());
        JSONObject jsonObject = PlatformPageUtils.formatPageData(fxBuildingsPage);
        return jsonObject;
    }
    private FxBuildingsEntity wrapBuildingEntity (String id, String province, String city, String area, Float buildingArea, String structureType, String buildingName,
                                                  Integer cs, Integer peopleNumber, String purpose, String buildingTime, String currentIntensity) {

        FxBuildingsEntity fxBuildingsEntity = new FxBuildingsEntity();
        fxBuildingsEntity.setId(id);
        fxBuildingsEntity.setProvince(province);
        fxBuildingsEntity.setCity(city);
        fxBuildingsEntity.setArea(area);
        fxBuildingsEntity.setBuildingArea(buildingArea);
        fxBuildingsEntity.setStructureType(structureType);
        fxBuildingsEntity.setBuildingName(buildingName);
        fxBuildingsEntity.setFloorNumber(cs);
        fxBuildingsEntity.setPeopleNumber(peopleNumber);
        fxBuildingsEntity.setPurpose(purpose);
        fxBuildingsEntity.setYear(buildingTime);
        fxBuildingsEntity.setCurrentIntensity(currentIntensity);

        return fxBuildingsEntity;
    }




    private Map<String, Map<String, Map<String, Map<String, Float>>>> getFxVulnerabilityProvince(FxMiddleResultQueryParams queryParams) {
        //{省-市-区县:
        //  {房屋结构类型: { 破坏程度 : {烈度: 易损性} }
        //  }
        Map<String, Map<String, Map<String, Map<String, Float>>>> fxVulnerabilityProvince = new HashMap<>();

        //  {房屋结构类型: {
        //      破坏程度 : {烈度: 易损性}
        //      }
        //  }
        //Map<String, Map<String, Map<String, Float>>> type2map = new HashMap<>();

        //  {
        //      破坏程度 : {烈度: 易损性}
        //  }
        //Map<String, Map<String, Float>> damageDegree2map = new HashMap<>();

        FxVulnerabilityProvinceQueryParams fxVulnerabilityProvinceQueryParams = new FxVulnerabilityProvinceQueryParams();
        fxVulnerabilityProvinceQueryParams.setProvince(queryParams.getProvince());
        fxVulnerabilityProvinceQueryParams.setCity(queryParams.getCity());
        fxVulnerabilityProvinceQueryParams.setArea(queryParams.getArea());
        List<FxVulnerabilityProvinceEntity> fxVulnerabilityProvinceEntities = fxVulnerabilityProvinceNativeRepository.listFxVulnerabilityProvinces(fxVulnerabilityProvinceQueryParams);

        Map<String, Map<String, FxVulnerabilityProvinceEntity>> structureTypeToDamageDegree2vulnerability = new HashMap<>();
        FxVulnerabilityProvinceEntity fxVulnerabilityProvinceEntity = null;
        String province = "", city = "", area = "", district = "", damageDegree = "", structureType = "";
        Float vVI = 0.0f, vVII = 0.0f, vVIII = 0.0f, vIX = 0.0f, vX = 0.0f;
        for (int i = 0; i < fxVulnerabilityProvinceEntities.size(); i++) {
            fxVulnerabilityProvinceEntity = fxVulnerabilityProvinceEntities.get(i);
            province = fxVulnerabilityProvinceEntity.getProvince();
            city = fxVulnerabilityProvinceEntity.getCity();
            area = fxVulnerabilityProvinceEntity.getArea();

            structureType = fxVulnerabilityProvinceEntity.getStructureType();
            damageDegree = fxVulnerabilityProvinceEntity.getDamageDegree();

            vVI = fxVulnerabilityProvinceEntity.getVulnerabilityVI();
            vVII = fxVulnerabilityProvinceEntity.getVulnerabilityVII();
            vVIII = fxVulnerabilityProvinceEntity.getVulnerabilityVIII();
            vIX = fxVulnerabilityProvinceEntity.getVulnerabilityIX();
            vX = fxVulnerabilityProvinceEntity.getVulnerabilityX();

            district = province + "-" + city + "-" + area;
            if (!fxVulnerabilityProvince.containsKey(district)) {
                Map<String, Map<String, Map<String, Float>>> type2map = new HashMap<>();
                Map<String, Map<String, Float>> damageDegree2map = new HashMap<>();
                Map<String, Float> intensity2vulnerability = new HashMap<>();
                intensity2vulnerability.put("6", vVI);
                intensity2vulnerability.put("7", vVII);
                intensity2vulnerability.put("8", vVIII);
                intensity2vulnerability.put("9", vIX);
                intensity2vulnerability.put("10", vX);
                damageDegree2map.put(damageDegree, intensity2vulnerability);
                type2map.put(structureType, damageDegree2map);
                fxVulnerabilityProvince.put(district, type2map);
            } else {
                Map<String, Map<String, Map<String, Float>>> type2map = fxVulnerabilityProvince.get(district);
                if (!type2map.containsKey(structureType)) {
                    Map<String, Map<String, Float>> damageDegree2map = new HashMap<>();
                    Map<String, Float> intensity2vulnerability = new HashMap<>();
                    intensity2vulnerability.put("6", vVI);
                    intensity2vulnerability.put("7", vVII);
                    intensity2vulnerability.put("8", vVIII);
                    intensity2vulnerability.put("9", vIX);
                    intensity2vulnerability.put("10", vX);
                    damageDegree2map.put(damageDegree, intensity2vulnerability);
                    type2map.put(structureType, damageDegree2map);
                } else {
                    Map<String, Map<String, Float>> damageDegree2map = type2map.get(structureType);
                    if (!damageDegree2map.containsKey(damageDegree)) {
                        Map<String, Float> intensity2vulnerability = new HashMap<>();
                        intensity2vulnerability.put("6", vVI);
                        intensity2vulnerability.put("7", vVII);
                        intensity2vulnerability.put("8", vVIII);
                        intensity2vulnerability.put("9", vIX);
                        intensity2vulnerability.put("10", vX);
                        damageDegree2map.put(damageDegree, intensity2vulnerability);
                    } else {
                        System.out.println(district + ", structureType: " + structureType + ", damageDegree: " + damageDegree + ", 出现冗余数据!");
                    }
                }
            }
        }
        return fxVulnerabilityProvince;
    }


    @Override
    public List<DictItemEntity> getValidDictItemsByDictCode(String dictCode) {
        List<DictItemEntity> validDictItems = PlatformAPI.getDictAPI().getValidDictItemsByDictCode(dictCode);
        return validDictItems;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public FxBuildingsEntity updateFxCensusBuildings(FxBuildingsEntity fxBuildingsEntity) {
        fxBuildingsEntity.setUpdateUser(PlatformSessionUtils.getUserId());
        fxBuildingsEntity.setUpdateTime(PlatformDateUtils.getCurrentTimestamp());
        String id = fxBuildingsEntity.getId();

        FxCityHouseEntity f1 = null;
        FxCityNonHouseEntity f2 = null;
        FxCountryDlHouseEntity f3 = null;
        FxCountryJhHouseEntity f4 = null;
        FxCountryNonHouseEntity f5 = null;

        f1 = fxCityHouseRepository.findById(id).get();
        if (f1 == null) {
            f2 = fxCityNonHouseRepository.findById(id).get();
            if (f2 == null) {
                f3 = fxCountryDlHouseRepository.findById(id).get();
                if (f3 == null) {
                    f4 = fxCountryJhHouseRepository.findById(id).get();
                    if (f4 == null) {
                        f5 = fxCountryNonHouseRepository.findById(id).get();
                    }
                }
            }
        }
        /*BeanUtil.copyPropertiesIgnoreNull(fxBuildingsEntity, one);
        this.fxCityHouseRepository.save(one);*/
        return fxBuildingsEntity;
    }

    @Override
    public FxBuildingsEntity getFxCensusBuildings(String id, String houseType) {

        FxCityHouseEntity fxCityHouseEntity = null;
        FxCityNonHouseEntity fxCityNonHouseEntity = null;
        FxCountryDlHouseEntity fxCountryDlHouseEntity = null;
        FxCountryJhHouseEntity fxCountryJhHouseEntity = null;
        FxCountryNonHouseEntity fxCountryNonHouseEntity = null;

        String uuid = "", province = "", city = "", area = "", structureType = "", buildingName = "", purpose = "",
                buildingTime = "", currentIntensity = "";
        Integer cs = 0, peopleNumber = 0;
        Float buildingArea = 0.0f;
        if ("city_house".equals(houseType)) {
            fxCityHouseEntity = this.fxCityHouseRepository.findById(id).orElse(null);
            uuid = fxCityHouseEntity.getBh();
            province = fxCityHouseEntity.getProvince();
            city = fxCityHouseEntity.getCity();
            area = fxCityHouseEntity.getDistrict();
            String dcmj = fxCityHouseEntity.getDcmj();
            buildingArea = (dcmj == null ? 0.0f : Float.parseFloat(dcmj));
            structureType = fxCityHouseEntity.getCzfwjglx();
            buildingName = fxCityHouseEntity.getMc();
            cs = fxCityHouseEntity.getDscs();
            peopleNumber = 0;
            purpose = "";
            buildingTime = fxCityHouseEntity.getBuildTime();
            currentIntensity = fxCityHouseEntity.getMqsfld();
        } else if ("city_non_house".equals(houseType)) {
            fxCityNonHouseEntity = this.fxCityNonHouseRepository.findById(id).orElse(null);
            uuid = fxCityNonHouseEntity.getBh();
            province = fxCityNonHouseEntity.getProvince();
            city = fxCityNonHouseEntity.getCity();
            area = fxCityNonHouseEntity.getDistrict();
            String dcmj = fxCityNonHouseEntity.getDcmj();
            buildingArea = (dcmj == null ? 0.0f : Float.parseFloat(dcmj));
            structureType = fxCityNonHouseEntity.getCzfwjglx();
            buildingName = fxCityNonHouseEntity.getMc();
            cs = fxCityNonHouseEntity.getDscs();
            peopleNumber = 0;
            purpose = "";
            buildingTime = fxCityNonHouseEntity.getBuildTime();
            currentIntensity = fxCityNonHouseEntity.getMqsfld();
        } else if ("country_dl_house".equals(houseType)) {
            fxCountryDlHouseEntity = this.fxCountryDlHouseRepository.findById(id).orElse(null);
            uuid = fxCountryDlHouseEntity.getBh();
            province = fxCountryDlHouseEntity.getProvince();
            city = fxCountryDlHouseEntity.getCity();
            area = fxCountryDlHouseEntity.getDistrict();
            String dcmj = fxCountryDlHouseEntity.getDcmj();
            buildingArea = (dcmj == null ? 0.0f : Float.parseFloat(dcmj));
            structureType = fxCountryDlHouseEntity.getJglx();
            buildingName = "";
            cs = fxCountryDlHouseEntity.getCs();
            peopleNumber = fxCountryDlHouseEntity.getCzrk();
            purpose = "";
            buildingTime = fxCountryDlHouseEntity.getBuildTime();
            currentIntensity = "";
        } else if ("country_jh_house".equals(houseType)) {
            fxCountryJhHouseEntity = this.fxCountryJhHouseRepository.findById(id).orElse(null);
            uuid = fxCountryJhHouseEntity.getBh();
            province = fxCountryJhHouseEntity.getProvince();
            city = fxCountryJhHouseEntity.getCity();
            area = fxCountryJhHouseEntity.getDistrict();
            String dcmj = fxCountryJhHouseEntity.getDcmj();
            buildingArea = (dcmj == null ? 0.0f : Float.parseFloat(dcmj));
            structureType = fxCountryJhHouseEntity.getJglx();
            buildingName = fxCountryJhHouseEntity.getJzmc();
            cs = fxCountryJhHouseEntity.getDscs();
            peopleNumber = 0;
            purpose = "";
            buildingTime = fxCountryJhHouseEntity.getBuildTime();
            currentIntensity = "";
        } else if ("country_non_house".equals(houseType)) {
            fxCountryNonHouseEntity = this.fxCountryNonHouseRepository.findById(id).orElse(null);
            uuid = fxCountryNonHouseEntity.getBh();
            province = fxCountryNonHouseEntity.getProvince();
            city = fxCountryNonHouseEntity.getCity();
            area = fxCountryNonHouseEntity.getDistrict();
            String dcmj = fxCountryNonHouseEntity.getDcmj();
            buildingArea = (dcmj == null ? 0.0f : Float.parseFloat(dcmj));
            structureType = fxCountryNonHouseEntity.getJglx();
            buildingName = fxCountryNonHouseEntity.getJzmc();
            cs = fxCountryNonHouseEntity.getDscs();
            peopleNumber = 0;
            purpose = fxCountryNonHouseEntity.getFwyt();
            buildingTime = fxCountryNonHouseEntity.getBuildTime();
            currentIntensity = "";
        }

        FxBuildingsEntity fxBuildingsEntity = wrapBuildingEntity (uuid, province, city, area, buildingArea, structureType, buildingName, cs, peopleNumber, purpose, buildingTime, currentIntensity);
        return fxBuildingsEntity;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteFxCensusBuildings(List<String> ids, String houseType) {
        if ("city_house".equals(houseType)) {
            List<FxCityHouseEntity> fxBuildingsList = this.fxCityHouseRepository.findAllById(ids);
            if (!PlatformObjectUtils.isEmpty(fxBuildingsList) && fxBuildingsList.size() > 0) {
                for (FxCityHouseEntity fxBuilding : fxBuildingsList) {
                    this.fxCityHouseRepository.delete(fxBuilding);
                }
            }
        } else if ("city_non_house".equals(houseType)) {
            List<FxCityNonHouseEntity> fxBuildingsList = this.fxCityNonHouseRepository.findAllById(ids);
            if (!PlatformObjectUtils.isEmpty(fxBuildingsList) && fxBuildingsList.size() > 0) {
                for (FxCityNonHouseEntity fxBuilding : fxBuildingsList) {
                    this.fxCityNonHouseRepository.delete(fxBuilding);
                }
            }
        } else if ("country_dl_house".equals(houseType)) {
            List<FxCountryDlHouseEntity> fxBuildingsList = this.fxCountryDlHouseRepository.findAllById(ids);
            if (!PlatformObjectUtils.isEmpty(fxBuildingsList) && fxBuildingsList.size() > 0) {
                for (FxCountryDlHouseEntity fxBuilding : fxBuildingsList) {
                    this.fxCountryDlHouseRepository.delete(fxBuilding);
                }
            }
        } else if ("country_jh_house".equals(houseType)) {
            List<FxCountryJhHouseEntity> fxBuildingsList = this.fxCountryJhHouseRepository.findAllById(ids);
            if (!PlatformObjectUtils.isEmpty(fxBuildingsList) && fxBuildingsList.size() > 0) {
                for (FxCountryJhHouseEntity fxBuilding : fxBuildingsList) {
                    this.fxCountryJhHouseRepository.delete(fxBuilding);
                }
            }
        } else if ("country_non_house".equals(houseType)) {
            List<FxCountryNonHouseEntity> fxBuildingsList = this.fxCountryNonHouseRepository.findAllById(ids);
            if (!PlatformObjectUtils.isEmpty(fxBuildingsList) && fxBuildingsList.size() > 0) {
                for (FxCountryNonHouseEntity fxBuilding : fxBuildingsList) {
                    this.fxCountryNonHouseRepository.delete(fxBuilding);
                }
            }
        }
    }








    private String getJglxName (String table, String code) {
        if (tableTojglx2Name.size() == 0) tableTojglx2Name = initCode2JglxName();
        return tableTojglx2Name.get(table).get(code);
    }
    private Map<String, Map<String, String>> initCode2JglxName() {

        Map<String, String> cityHouse_jglx2Name = new HashMap<>(5);
        cityHouse_jglx2Name.put("1", "砌体结构");
        cityHouse_jglx2Name.put("4", "钢筋混凝土结构");
        cityHouse_jglx2Name.put("5", "钢结构");
        cityHouse_jglx2Name.put("6", "木结构");
        cityHouse_jglx2Name.put("99999", "其它");
        tableTojglx2Name.put("city_house", cityHouse_jglx2Name);

        Map<String, String> cityNonHouse_jglx2Name = new HashMap<>(5);
        cityNonHouse_jglx2Name.put("1001", "砌体结构");
        cityNonHouse_jglx2Name.put("2003", "钢筋混凝土结构");
        cityNonHouse_jglx2Name.put("4", "钢结构");
        cityNonHouse_jglx2Name.put("5", "木结构");
        cityNonHouse_jglx2Name.put("99999", "其它");
        tableTojglx2Name.put("city_non_house", cityNonHouse_jglx2Name);

        Map<String, String> countryDlHouse_jglx2Name = new HashMap<>(9);
        countryDlHouse_jglx2Name.put("1", "砌体结构");
        countryDlHouse_jglx2Name.put("3", "土木/石木结构");
        countryDlHouse_jglx2Name.put("10", "底部框架-抗震墙砌体结构");
        countryDlHouse_jglx2Name.put("5", "木（竹）结构");
        countryDlHouse_jglx2Name.put("6", "窑洞");
        countryDlHouse_jglx2Name.put("7", "混杂结构");
        countryDlHouse_jglx2Name.put("8", "钢筋混凝土结构");
        countryDlHouse_jglx2Name.put("9", "钢结构");
        countryDlHouse_jglx2Name.put("99999", "其他");
        tableTojglx2Name.put("country_dl_house", countryDlHouse_jglx2Name);

        Map<String, String> countryJhHouse_jglx2Name = new HashMap<>(6);
        countryJhHouse_jglx2Name.put("1", "砌体结构");
        countryJhHouse_jglx2Name.put("2", "底部框架-抗震墙砌体结构");
        countryJhHouse_jglx2Name.put("3", "钢筋混凝土结构");
        countryJhHouse_jglx2Name.put("4", "钢结构");
        countryJhHouse_jglx2Name.put("5", "木结构");
        countryJhHouse_jglx2Name.put("6", "其它");
        tableTojglx2Name.put("country_jh_house", countryJhHouse_jglx2Name);

        Map<String, String> countryNonHouse_jglx2Name = new HashMap<>(9);
        countryNonHouse_jglx2Name.put("1", "砌体结构");
        countryNonHouse_jglx2Name.put("3", "钢筋混凝土结构");
        countryNonHouse_jglx2Name.put("4", "钢结构");
        countryNonHouse_jglx2Name.put("12", "底部框架-抗震墙砌体结构");
        countryNonHouse_jglx2Name.put("6", "土木/石木结构");
        countryNonHouse_jglx2Name.put("8", "木（竹）结构");
        countryNonHouse_jglx2Name.put("9", "窑洞");
        countryNonHouse_jglx2Name.put("10", "混杂结构");
        countryNonHouse_jglx2Name.put("11", "其他");
        tableTojglx2Name.put("country_non_house", countryNonHouse_jglx2Name);

        return tableTojglx2Name;
    }

    private String getFwytName (String table, String code) {
        if (tableTofwyt2Name.size() == 0) tableTofwyt2Name = initCode2FwytName();
        return tableTofwyt2Name.get(table).get(code);
    }
    private Map<String, Map<String, String>> initCode2FwytName() {

        Map<String, String> cityHouse_fwyt2Name = new HashMap<>(5);
        tableTofwyt2Name.put("city_house", cityHouse_fwyt2Name);

        Map<String, String> cityNonHouse_fwyt2Name = new HashMap<>(17);
        cityNonHouse_fwyt2Name.put("1001", "中小学幼儿园教学楼宿舍楼等教育建筑");
        cityNonHouse_fwyt2Name.put("2", "其他学校建筑");
        cityNonHouse_fwyt2Name.put("1003", "医疗建筑");
        cityNonHouse_fwyt2Name.put("1004", "福利院");
        cityNonHouse_fwyt2Name.put("1005", "养老建筑");
        cityNonHouse_fwyt2Name.put("1006", "疾控、消防等救灾建筑");
        cityNonHouse_fwyt2Name.put("17", "纪念建筑");
        cityNonHouse_fwyt2Name.put("18", "宗教建筑");
        cityNonHouse_fwyt2Name.put("1009", "文化建筑");
        cityNonHouse_fwyt2Name.put("19", "综合建筑");
        cityNonHouse_fwyt2Name.put("1011", "商业建筑");
        cityNonHouse_fwyt2Name.put("1012", "体育建筑");
        cityNonHouse_fwyt2Name.put("1013", "通信电力交通邮电广播电视等基础设施建筑");
        cityNonHouse_fwyt2Name.put("14", "工业建筑");
        cityNonHouse_fwyt2Name.put("15", "办公建筑");
        cityNonHouse_fwyt2Name.put("16", "仓储建筑");
        cityNonHouse_fwyt2Name.put("99999", "其它");
        tableTofwyt2Name.put("city_non_house", cityNonHouse_fwyt2Name);

        Map<String, String> countryDlHouse_fwyt2Name = new HashMap<>(9);
        tableTofwyt2Name.put("country_dl_house", countryDlHouse_fwyt2Name);

        Map<String, String> countryJhHouse_fwyt2Name = new HashMap<>(9);
        tableTofwyt2Name.put("country_jh_house", countryJhHouse_fwyt2Name);

        Map<String, String> countryNonHouse_fwyt2Name = new HashMap<>(3);
        countryNonHouse_fwyt2Name.put("1", "公共服务建筑");
        countryNonHouse_fwyt2Name.put("2", "商业建筑");
        countryNonHouse_fwyt2Name.put("3", "工业建筑");
        tableTofwyt2Name.put("country_non_house", countryNonHouse_fwyt2Name);

        return tableTofwyt2Name;
    }

    private String getMqsfldName (String table, String code) {
        if (tableTomqsfld2Name.size() == 0) tableTomqsfld2Name = initCode2MqsfldName();
        return tableTomqsfld2Name.get(table).get(code);
    }
    private Map<String, Map<String, String>> initCode2MqsfldName() {

        Map<String, String> cityHouse_mqsfld2Name = new HashMap<>(6);
        cityHouse_mqsfld2Name.put("1", "6度(0.05g)");
        cityHouse_mqsfld2Name.put("2", "7度(0.10g)");
        cityHouse_mqsfld2Name.put("3", "7度(0.15g)");
        cityHouse_mqsfld2Name.put("4", "8度(0.20g)");
        cityHouse_mqsfld2Name.put("5", "8度(0.30g)");
        cityHouse_mqsfld2Name.put("6", "9度(0.40g)");
        tableTomqsfld2Name.put("city_house", cityHouse_mqsfld2Name);

        Map<String, String> cityNonHouse_mqsfld2Name = new HashMap<>(6);
        cityNonHouse_mqsfld2Name.put("1", "6度(0.05g)");
        cityNonHouse_mqsfld2Name.put("2", "7度(0.10g)");
        cityNonHouse_mqsfld2Name.put("3", "7度(0.15g)");
        cityNonHouse_mqsfld2Name.put("4", "8度(0.20g)");
        cityNonHouse_mqsfld2Name.put("5", "8度(0.30g)");
        cityNonHouse_mqsfld2Name.put("6", "9度(0.40g)");
        tableTomqsfld2Name.put("city_non_house", cityNonHouse_mqsfld2Name);

        Map<String, String> countryDlHouse_mqsfld2Name = new HashMap<>(9);
        tableTomqsfld2Name.put("country_dl_house", countryDlHouse_mqsfld2Name);

        Map<String, String> countryJhHouse_mqsfld2Name = new HashMap<>(9);
        tableTomqsfld2Name.put("country_jh_house", countryJhHouse_mqsfld2Name);

        Map<String, String> countryNonHouse_mqsfld2Name = new HashMap<>(3);
        tableTomqsfld2Name.put("country_non_house", countryNonHouse_mqsfld2Name);

        return tableTomqsfld2Name;
    }

    private String getDivisionNameByCode (String level, String code) {
        if (divisionCodeToName.size() == 0) divisionCodeToName = initDivisionNameByCode();
        return divisionCodeToName.get(level).get(code);
    }
    private Map<String, Map<String, String>> initDivisionNameByCode () {
        Map<String, Map<String, String>> divisionCodeToName = new HashMap<>();

        Map<String, String> provinceCodeToName = new HashMap<>();
        Map<String, String> cityCodeToName = new HashMap<>();
        Map<String, String> districtCodeToName = new HashMap<>();

        String divisionId = "", provinceCode = "", provinceName = "", cityCode = "", cityName = "", districtCode = "", districtName = "";
        DivisionEntity divisionEntity = null;
        List<DivisionEntity> divisionEntities_1 = PlatformAPI.getDivisionAPI().getSubDivisions("root");
        for (int i = 0; i < divisionEntities_1.size(); i++) {
            divisionEntity = divisionEntities_1.get(i);
            provinceCode = divisionEntity.getDivisionCode();
            provinceName = divisionEntity.getDivisionName();
            provinceCodeToName.put(provinceCode, provinceName);

            divisionId = divisionEntity.getDivisionId();
            List<DivisionEntity> divisionEntities_2 = PlatformAPI.getDivisionAPI().getSubDivisions(divisionId);
            for (int j = 0; j < divisionEntities_2.size(); j++) {
                divisionEntity = divisionEntities_2.get(j);
                cityCode = divisionEntity.getDivisionCode();
                cityName = divisionEntity.getDivisionName();
                cityCodeToName.put(cityCode, cityName);

                divisionId = divisionEntity.getDivisionId();
                List<DivisionEntity> divisionEntities_3 = PlatformAPI.getDivisionAPI().getSubDivisions(divisionId);
                for (int k = 0; k < divisionEntities_3.size(); k++) {
                    divisionEntity = divisionEntities_3.get(k);
                    districtCode = divisionEntity.getDivisionCode();
                    districtName = divisionEntity.getDivisionName();
                    districtCodeToName.put(districtCode, districtName);
                }
            }
        }
        divisionCodeToName.put("province", provinceCodeToName);
        divisionCodeToName.put("city", cityCodeToName);
        divisionCodeToName.put("district", districtCodeToName);
        return divisionCodeToName;
    }












    /*************************************************/
    private List<FxBuildingsEntity> getFxAllHouses (FxHouseQueryParams fxHouseQueryParams, String userId) {

        List<FxBuildingsEntity> fxBuildings = new ArrayList<>();

        List<FxBuildingsEntity> fxCityHouses = getFxCityHouses(fxHouseQueryParams, userId);

        List<FxBuildingsEntity> fxCityNonHouses = getFxCityNonHouses(fxHouseQueryParams, userId);

        List<FxBuildingsEntity> fxCountryDlHouses = getFxCountryDlHouses(fxHouseQueryParams, userId);

        List<FxBuildingsEntity> fxCountryJhHouses = getFxCountryJhHouses(fxHouseQueryParams, userId);

        List<FxBuildingsEntity> fxCountryNonHouses = getFxCountryNonHouses(fxHouseQueryParams, userId);

        fxBuildings.addAll(fxCityHouses);
        fxBuildings.addAll(fxCityNonHouses);
        fxBuildings.addAll(fxCountryDlHouses);
        fxBuildings.addAll(fxCountryJhHouses);
        fxBuildings.addAll(fxCountryNonHouses);

        return fxBuildings;
    }

    private List<FxBuildingsEntity> getFxCityHouses (FxHouseQueryParams fxCityHouseQueryParams, String userId) {
        List<FxBuildingsEntity> fxBuildings = new ArrayList<>();

        String province = "", city = "", area = "";
        List<FxCityHouseEntity> fxCityHouseEntities = fxCityHouseNativeRepository.listFxCityHouses(fxCityHouseQueryParams, userId);
        FxCityHouseEntity fxCityHouseEntity = null;
        for (int i = 0; i < fxCityHouseEntities.size(); i++) {
            fxCityHouseEntity = fxCityHouseEntities.get(i);

            province = fxCityHouseEntity.getProvince();
            city = fxCityHouseEntity.getCity();
            area = fxCityHouseEntity.getDistrict();

            Float dcmj = new Float(fxCityHouseEntity.getDcmj());//建筑面积
            String czfwjglx = fxCityHouseEntity.getCzfwjglx();//结构类型
            String shape = fxCityHouseEntity.getShape();

            FxBuildingsEntity fxBuildingsEntity = new FxBuildingsEntity();
            fxBuildingsEntity.setProvince(province);
            fxBuildingsEntity.setCity(city);
            fxBuildingsEntity.setArea(area);
            fxBuildingsEntity.setBuildingArea(dcmj);
            fxBuildingsEntity.setStructureType(czfwjglx);
            fxBuildingsEntity.setCoordinate(shape);

            fxBuildings.add(fxBuildingsEntity);
        }
        return fxBuildings;
    }

    private List<FxBuildingsEntity> getFxCityNonHouses (FxHouseQueryParams fxCityHouseQueryParams, String userId) {
        List<FxBuildingsEntity> fxBuildings = new ArrayList<>();

        String province = "", city = "", area = "";
        List<FxCityNonHouseEntity> fxCityHouseEntities = fxCityNonHouseNativeRepository.listFxCityNonHouses(fxCityHouseQueryParams, userId);
        FxCityNonHouseEntity fxCityNonHouseEntity = null;
        for (int i = 0; i < fxCityHouseEntities.size(); i++) {
            fxCityNonHouseEntity = fxCityHouseEntities.get(i);

            province = fxCityNonHouseEntity.getProvince();
            city = fxCityNonHouseEntity.getCity();
            area = fxCityNonHouseEntity.getDistrict();

            Float dcmj = new Float(fxCityNonHouseEntity.getDcmj());//建筑面积
            String czfwjglx = fxCityNonHouseEntity.getCzfwjglx();//结构类型
            String shape = fxCityNonHouseEntity.getShape();

            FxBuildingsEntity fxBuildingsEntity = new FxBuildingsEntity();
            fxBuildingsEntity.setProvince(province);
            fxBuildingsEntity.setCity(city);
            fxBuildingsEntity.setArea(area);
            fxBuildingsEntity.setBuildingArea(dcmj);
            fxBuildingsEntity.setStructureType(czfwjglx);
            fxBuildingsEntity.setCoordinate(shape);

            fxBuildings.add(fxBuildingsEntity);
        }
        return fxBuildings;
    }

    private List<FxBuildingsEntity> getFxCountryDlHouses (FxHouseQueryParams fxCityHouseQueryParams, String userId) {
        List<FxBuildingsEntity> fxBuildings = new ArrayList<>();

        String province = "", city = "", area = "";
        List<FxCountryDlHouseEntity> fxCountryDlHouseEntities = fxCountryDlHouseNativeRepository.listFxCountryDlHouses(fxCityHouseQueryParams, userId);
        FxCountryDlHouseEntity fxCountryDlHouseEntity = null;
        for (int i = 0; i < fxCountryDlHouseEntities.size(); i++) {
            fxCountryDlHouseEntity = fxCountryDlHouseEntities.get(i);

            province = fxCountryDlHouseEntity.getProvince();
            city = fxCountryDlHouseEntity.getCity();
            area = fxCountryDlHouseEntity.getDistrict();

            Float dcmj = new Float(fxCountryDlHouseEntity.getDcmj());//建筑面积
            String czfwjglx = fxCountryDlHouseEntity.getJglx();//结构类型
            String shape = fxCountryDlHouseEntity.getShape();

            FxBuildingsEntity fxBuildingsEntity = new FxBuildingsEntity();
            fxBuildingsEntity.setProvince(province);
            fxBuildingsEntity.setCity(city);
            fxBuildingsEntity.setArea(area);
            fxBuildingsEntity.setBuildingArea(dcmj);
            fxBuildingsEntity.setStructureType(czfwjglx);
            fxBuildingsEntity.setCoordinate(shape);

            fxBuildings.add(fxBuildingsEntity);
        }
        return fxBuildings;
    }

    private List<FxBuildingsEntity> getFxCountryFzHouses (FxHouseQueryParams fxCityHouseQueryParams, String userId) {
        List<FxBuildingsEntity> fxBuildings = new ArrayList<>();

        /*String province = "", city = "", area = "";
        List<FxCountryFzHouseEntity> fxCountryDlHouseEntities = fxCountryFzHouseNativeRepository.listFxCountryFzHouses(fxCityHouseQueryParams);
        FxCountryFzHouseEntity fxCountryDlHouseEntity = null;
        for (int i = 0; i < fxCountryDlHouseEntities.size(); i++) {
            fxCountryDlHouseEntity = fxCountryDlHouseEntities.get(i);

            province = fxCountryDlHouseEntity.getProvince();
            city = fxCountryDlHouseEntity.getCity();
            area = fxCountryDlHouseEntity.getDistrict();

            Float dcmj = fxCountryDlHouseEntity.getDcmj();//建筑面积
            String czfwjglx = fxCountryDlHouseEntity.getCzfwjglx();//结构类型

            FxBuildingsEntity fxBuildingsEntity = new FxBuildingsEntity();
            fxBuildingsEntity.setProvince(province);
            fxBuildingsEntity.setCity(city);
            fxBuildingsEntity.setArea(area);
            fxBuildingsEntity.setBuildingArea(dcmj);
            fxBuildingsEntity.setStructureType(czfwjglx);

            fxBuildings.add(fxBuildingsEntity);
        }*/
        return fxBuildings;
    }

    private List<FxBuildingsEntity> getFxCountryJhHouses (FxHouseQueryParams fxCityHouseQueryParams, String userId) {
        List<FxBuildingsEntity> fxBuildings = new ArrayList<>();

        String province = "", city = "", area = "";
        List<FxCountryJhHouseEntity> fxCountryJhHouseEntities = fxCountryJhHouseNativeRepository.listFxCountryJhHouses(fxCityHouseQueryParams, userId);
        FxCountryJhHouseEntity fxCountryJhHouseEntity = null;
        for (int i = 0; i < fxCountryJhHouseEntities.size(); i++) {
            fxCountryJhHouseEntity = fxCountryJhHouseEntities.get(i);

            province = fxCountryJhHouseEntity.getProvince();
            city = fxCountryJhHouseEntity.getCity();
            area = fxCountryJhHouseEntity.getDistrict();

            Float dcmj = new Float(fxCountryJhHouseEntity.getDcmj());//建筑面积
            String czfwjglx = fxCountryJhHouseEntity.getJglx();//结构类型
            String shape = fxCountryJhHouseEntity.getShape();

            FxBuildingsEntity fxBuildingsEntity = new FxBuildingsEntity();
            fxBuildingsEntity.setProvince(province);
            fxBuildingsEntity.setCity(city);
            fxBuildingsEntity.setArea(area);
            fxBuildingsEntity.setBuildingArea(dcmj);
            fxBuildingsEntity.setStructureType(czfwjglx);
            fxBuildingsEntity.setCoordinate(shape);

            fxBuildings.add(fxBuildingsEntity);
        }
        return fxBuildings;
    }

    private List<FxBuildingsEntity> getFxCountryNonHouses (FxHouseQueryParams fxCityHouseQueryParams, String userId) {
        List<FxBuildingsEntity> fxBuildings = new ArrayList<>();

        String province = "", city = "", area = "";
        List<FxCountryNonHouseEntity> fxCountryNonHouseEntities = fxCountryNonHouseNativeRepository.listFxCountryNonHouses(fxCityHouseQueryParams, userId);
        FxCountryNonHouseEntity fxCountryNonHouseEntity = null;
        for (int i = 0; i < fxCountryNonHouseEntities.size(); i++) {
            fxCountryNonHouseEntity = fxCountryNonHouseEntities.get(i);

            province = fxCountryNonHouseEntity.getProvince();
            city = fxCountryNonHouseEntity.getCity();
            area = fxCountryNonHouseEntity.getDistrict();

            Float dcmj = new Float(fxCountryNonHouseEntity.getDcmj());//建筑面积
            String czfwjglx = fxCountryNonHouseEntity.getJglx();//结构类型
            String shape = fxCountryNonHouseEntity.getShape();

            FxBuildingsEntity fxBuildingsEntity = new FxBuildingsEntity();
            fxBuildingsEntity.setProvince(province);
            fxBuildingsEntity.setCity(city);
            fxBuildingsEntity.setArea(area);
            fxBuildingsEntity.setBuildingArea(dcmj);
            fxBuildingsEntity.setStructureType(czfwjglx);
            fxBuildingsEntity.setCoordinate(shape);

            fxBuildings.add(fxBuildingsEntity);
        }
        return fxBuildings;
    }

    /*************************************************/


    @Override
    public JSONObject searchFxCensusBuildings(FxHouseQueryParams queryParams, String userId) {

        JSONObject jsonObject = new JSONObject();

        String houseType = queryParams.getHouseType();
        List<FxBuildingsEntity> fxBuildingsEntities = null;
        if ("city_house".equals(houseType)) {
            fxBuildingsEntities = searchFxCensusCityHouses(queryParams, userId);
        } else if ("city_non_house".equals(houseType)) {
            fxBuildingsEntities = searchFxCensusCityNonHouses(queryParams, userId);
        } else if ("country_dl_house".equals(houseType)) {
            fxBuildingsEntities = searchFxCensusCountryDlHouses(queryParams, userId);
        } else if ("country_jh_house".equals(houseType)) {
            fxBuildingsEntities = searchFxCensusCountryJhHouses(queryParams, userId);
        } else if ("country_non_house".equals(houseType)) {
            fxBuildingsEntities = searchFxCensusCountryNonHouses(queryParams, userId);
        }
        jsonObject.put("data", fxBuildingsEntities);
        return jsonObject;
    }
    private List<FxBuildingsEntity> searchFxCensusCityHouses(FxHouseQueryParams fxHouseQueryParams, String userId) {

        String province = "", city = "", area = "";
        List<FxBuildingsEntity> fxBuildings = new ArrayList<>();

        List<FxCityHouseEntity> fxCityHouseList = fxCityHouseNativeRepository.searchFxCityHouses(fxHouseQueryParams, userId);
        FxCityHouseEntity fxCityHouseEntity = null;
        for (int i = 0; i < fxCityHouseList.size(); i++) {
            fxCityHouseEntity = fxCityHouseList.get(i);

            String id = fxCityHouseEntity.getBh();

            province = fxCityHouseEntity.getProvince();
            city = fxCityHouseEntity.getCity();
            area = fxCityHouseEntity.getDistrict();

            province = getDivisionNameByCode("province", province);
            city = getDivisionNameByCode("city", city);
            area = getDivisionNameByCode("district", area);

            String buildingName = fxCityHouseEntity.getJzmc();
            String mj = fxCityHouseEntity.getDcmj();
            Float dcmj = (mj == null ? 0.0f : new Float(mj));//建筑面积
            String czfwjglx = fxCityHouseEntity.getCzfwjglx();//结构类型
            czfwjglx = getJglxName("city_house", czfwjglx);
            Integer peopleNumber = 0;
            String purpose = "";
            Integer cs = fxCityHouseEntity.getDscs();
            String buildingTime = fxCityHouseEntity.getBuildTime();
            String currentIntensity = fxCityHouseEntity.getMqsfld();
            currentIntensity = getMqsfldName("city_house", currentIntensity);

            //province, city, area, buildingArea, structureType, buildingName, purpose, buildingTime, currentIntensity
            FxBuildingsEntity fxBuildingsEntity = wrapBuildingEntity(id, province, city, area, dcmj, czfwjglx, buildingName, cs, peopleNumber, purpose, buildingTime, currentIntensity);

            String shape = fxCityHouseEntity.getShape();
            shape = parseWktShape(shape);
            fxBuildingsEntity.setCoordinate(shape);

            fxBuildings.add(fxBuildingsEntity);
        }
        return fxBuildings;
    }
    private List<FxBuildingsEntity> searchFxCensusCityNonHouses(FxHouseQueryParams fxHouseQueryParams, String userId) {

        String province = "", city = "", area = "";
        List<FxBuildingsEntity> fxBuildings = new ArrayList<>();

        List<FxCityNonHouseEntity> fxCityNonHouseEntityList = fxCityNonHouseNativeRepository.searchFxCityNonHouses(fxHouseQueryParams, userId);
        FxCityNonHouseEntity fxCityNonHouseEntity = null;
        for (int i = 0; i < fxCityNonHouseEntityList.size(); i++) {
            fxCityNonHouseEntity = fxCityNonHouseEntityList.get(i);

            String id = fxCityNonHouseEntity.getBh();

            province = fxCityNonHouseEntity.getProvince();
            city = fxCityNonHouseEntity.getCity();
            area = fxCityNonHouseEntity.getDistrict();

            province = getDivisionNameByCode("province", province);
            city = getDivisionNameByCode("city", city);
            area = getDivisionNameByCode("district", area);

            String buildingName = fxCityNonHouseEntity.getJzmc();
            String mj = fxCityNonHouseEntity.getDcmj();
            Float dcmj = (mj == null ? 0.0f : new Float(mj));//建筑面积
            String czfwjglx = fxCityNonHouseEntity.getCzfwjglx();//结构类型
            czfwjglx = getJglxName("city_non_house", czfwjglx);
            Integer peopleNumber = 0;
            String purpose = String.valueOf(fxCityNonHouseEntity.getFwyt());
            purpose = getFwytName("city_non_house", purpose);
            Integer cs = fxCityNonHouseEntity.getDscs();
            String buildingTime = fxCityNonHouseEntity.getBuildTime();
            String currentIntensity = fxCityNonHouseEntity.getMqsfld();
            currentIntensity = getMqsfldName("city_non_house", currentIntensity);

            //province, city, area, buildingArea, structureType, buildingName, purpose, buildingTime, currentIntensity
            FxBuildingsEntity fxBuildingsEntity = wrapBuildingEntity(id, province, city, area, dcmj, czfwjglx, buildingName, cs, peopleNumber, purpose, buildingTime, currentIntensity);

            String shape = fxCityNonHouseEntity.getShape();
            shape = parseWktShape(shape);
            fxBuildingsEntity.setCoordinate(shape);

            fxBuildings.add(fxBuildingsEntity);
        }
        return fxBuildings;
    }
    private List<FxBuildingsEntity> searchFxCensusCountryDlHouses(FxHouseQueryParams fxHouseQueryParams, String userId) {

        String province = "", city = "", area = "";
        List<FxBuildingsEntity> fxBuildings = new ArrayList<>();

        List<FxCountryDlHouseEntity> fxCountryDlHouseEntityList = fxCountryDlHouseNativeRepository.searchFxCountryDlHouses(fxHouseQueryParams, userId);
        FxCountryDlHouseEntity fxCountryDlHouseEntity = null;
        for (int i = 0; i < fxCountryDlHouseEntityList.size(); i++) {
            fxCountryDlHouseEntity = fxCountryDlHouseEntityList.get(i);

            String id = fxCountryDlHouseEntity.getBh();

            province = fxCountryDlHouseEntity.getProvince();
            city = fxCountryDlHouseEntity.getCity();
            area = fxCountryDlHouseEntity.getDistrict();

            province = getDivisionNameByCode("province", province);
            city = getDivisionNameByCode("city", city);
            area = getDivisionNameByCode("district", area);

            String buildingName = fxCountryDlHouseEntity.getAddress();
            String mj = fxCountryDlHouseEntity.getDcmj();
            Float dcmj = (mj == null ? 0.0f : new Float(mj));//建筑面积
            String czfwjglx = fxCountryDlHouseEntity.getJglx();//结构类型
            czfwjglx = getJglxName("country_dl_house", czfwjglx);
            Integer peopleNumber = 0;
            String purpose = "";;
            Integer cs = fxCountryDlHouseEntity.getCs();
            String buildingTime = fxCountryDlHouseEntity.getBuildTime();
            String currentIntensity = "";

            //province, city, area, buildingArea, structureType, buildingName, purpose, buildingTime, currentIntensity
            FxBuildingsEntity fxBuildingsEntity = wrapBuildingEntity(id, province, city, area, dcmj, czfwjglx, buildingName, cs, peopleNumber, purpose, buildingTime, currentIntensity);

            String shape = fxCountryDlHouseEntity.getShape();
            shape = parseWktShape(shape);
            fxBuildingsEntity.setCoordinate(shape);

            fxBuildings.add(fxBuildingsEntity);
        }
        return fxBuildings;
    }
    private List<FxBuildingsEntity> searchFxCensusCountryJhHouses(FxHouseQueryParams fxHouseQueryParams, String userId) {

        String province = "", city = "", area = "";
        List<FxBuildingsEntity> fxBuildings = new ArrayList<>();

        List<FxCountryJhHouseEntity> fxCountryJhHouseEntityList = fxCountryJhHouseNativeRepository.searchFxCountryJhHouses(fxHouseQueryParams, userId);
        FxCountryJhHouseEntity fxCountryJhHouseEntity = null;
        for (int i = 0; i < fxCountryJhHouseEntityList.size(); i++) {
            fxCountryJhHouseEntity = fxCountryJhHouseEntityList.get(i);

            String id = fxCountryJhHouseEntity.getBh();

            province = fxCountryJhHouseEntity.getProvince();
            city = fxCountryJhHouseEntity.getCity();
            area = fxCountryJhHouseEntity.getDistrict();

            province = getDivisionNameByCode("province", province);
            city = getDivisionNameByCode("city", city);
            area = getDivisionNameByCode("district", area);

            String buildingName = fxCountryJhHouseEntity.getAddress();
            String mj = fxCountryJhHouseEntity.getDcmj();
            Float dcmj = (mj == null ? 0.0f : new Float(mj));//建筑面积
            String czfwjglx = fxCountryJhHouseEntity.getJglx();//结构类型
            czfwjglx = getJglxName("country_jh_house", czfwjglx);
            Integer peopleNumber = 0;
            String purpose = "";;
            Integer cs = fxCountryJhHouseEntity.getDscs();
            String buildingTime = fxCountryJhHouseEntity.getBuildTime();
            String currentIntensity = "";

            //province, city, area, buildingArea, structureType, buildingName, purpose, buildingTime, currentIntensity
            FxBuildingsEntity fxBuildingsEntity = wrapBuildingEntity(id, province, city, area, dcmj, czfwjglx, buildingName, cs, peopleNumber, purpose, buildingTime, currentIntensity);

            String shape = fxCountryJhHouseEntity.getShape();
            shape = parseWktShape(shape);
            fxBuildingsEntity.setCoordinate(shape);

            fxBuildings.add(fxBuildingsEntity);
        }
        return fxBuildings;
    }
    private List<FxBuildingsEntity> searchFxCensusCountryNonHouses(FxHouseQueryParams fxHouseQueryParams, String userId) {

        String province = "", city = "", area = "";
        List<FxBuildingsEntity> fxBuildings = new ArrayList<>();

        List<FxCountryNonHouseEntity> fxCountryNonHouseEntityList = fxCountryNonHouseNativeRepository.searchFxCountryNonHouses(fxHouseQueryParams, userId);
        FxCountryNonHouseEntity fxCountryNonHouseEntity = null;
        for (int i = 0; i < fxCountryNonHouseEntityList.size(); i++) {
            fxCountryNonHouseEntity = fxCountryNonHouseEntityList.get(i);

            String id = fxCountryNonHouseEntity.getBh();

            province = fxCountryNonHouseEntity.getProvince();
            city = fxCountryNonHouseEntity.getCity();
            area = fxCountryNonHouseEntity.getDistrict();

            province = getDivisionNameByCode("province", province);
            city = getDivisionNameByCode("city", city);
            area = getDivisionNameByCode("district", area);

            String buildingName = fxCountryNonHouseEntity.getAddress();
            String mj = fxCountryNonHouseEntity.getDcmj();
            Float dcmj = (mj == null ? 0.0f : new Float(mj));//建筑面积
            String czfwjglx = fxCountryNonHouseEntity.getJglx();//结构类型
            czfwjglx = getJglxName("country_jh_house", czfwjglx);
            Integer peopleNumber = 0;
            String purpose = "";;
            Integer cs = fxCountryNonHouseEntity.getDscs();
            String buildingTime = fxCountryNonHouseEntity.getBuildTime();
            String currentIntensity = "";

            //province, city, area, buildingArea, structureType, buildingName, purpose, buildingTime, currentIntensity
            FxBuildingsEntity fxBuildingsEntity = wrapBuildingEntity(id, province, city, area, dcmj, czfwjglx, buildingName, cs, peopleNumber, purpose, buildingTime, currentIntensity);

            String shape = fxCountryNonHouseEntity.getShape();
            shape = parseWktShape(shape);
            fxBuildingsEntity.setCoordinate(shape);

            fxBuildings.add(fxBuildingsEntity);
        }
        return fxBuildings;
    }


    private static String parseWktShape(String shape) {
        if (shape == null) return "";
        shape = shape.substring(shape.indexOf(";") + 1);
        shape = shape.toLowerCase();
        if (shape.indexOf("multipolygon") != -1) {
            shape = shape.substring(shape.indexOf("(((") + 3, shape.lastIndexOf(")))") - 1);
            shape = shape.replace(")), ((", "|");
            shape = shape.replace("), (", "#");
            return shape;
        } else if (shape.indexOf("polygon") != -1) {
            String sp = shape.indexOf("),(") != -1 ? "),(" : (shape.indexOf("), (") != -1 ? "), (" : "");
            String oldWkt = shape, coordinate = "";
            if ("".equals(sp)) {
                oldWkt = oldWkt.substring(oldWkt.indexOf("(") + 2, oldWkt.lastIndexOf("))"));
                String[] coordinates = oldWkt.split(",");
                for (int i = 0; i < coordinates.length; i++) {
                    String[] _coords = coordinates[i].split(" ");
                    coordinate += _coords[0] + " " + _coords[1] + ",";
                }
                coordinate = coordinate.substring(0, coordinate.length() - 1);
                return coordinate;
            } else {
                shape = shape.substring(shape.indexOf("((") + 2, shape.indexOf("))") - 1);
                oldWkt  = shape;
                oldWkt = oldWkt.replace(sp, "#"); sp = "#";
                String[] coordinates = oldWkt.split(sp); String tempCoordinate = "";
                for (int i = 0; i < coordinates.length; i++) {
                    tempCoordinate = coordinates[i];
                    String[] _coords = tempCoordinate.split(","), __coords = null;
                    for (int j = 0; j < _coords.length; j++) {
                        __coords = _coords[j].split(" ");
                        coordinate += __coords[0] + " " + __coords[1] + ",";
                    }
                    coordinate = coordinate.substring(0, coordinate.length() - 1) + "|";
                }
                coordinate = coordinate.substring(0, coordinate.length() - 1);
                return coordinate;
            }
        }
        return "";
    }



}
